import axios from 'axios'
import Qs from 'qs'
// const Url ='http://service.ruizhixue.cn/ht/yiru/index.php/wap/'
export function initAxios(Token) {
  axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
  axios.defaults.headers.token = Token
}
export function getData(hasParamUrl, callback, data) {
  if (hasParamUrl == null) return
  axios.get(hasParamUrl, {
    params: data
  }).then(function (response) {
    console.log(response)
    backView(response, callback)
  }).catch(function (error) {
    console.log(error)
  })
}
export function postData(requestUrl, data, callback, sssss) {
  var data
  sssss ? data=data : data = Qs.stringify(data)
  axios.post(requestUrl, data).then(function (response) {
    console.log(response)
    backView(response, callback)
  }).catch(function (error) {
    console.log(error)
  })
}

function backView(response, callback) {
  // console.log(response)
  if (response.data.code === 501) {
    // window.location.href = 'http://service.ruizhixue.cn/ht/webyirup/#/Login'
    callback(response.data)
  } else if (response.data.code === 200) {
    callback(response.data)
  } else {
    callback(response.data)
  }
}

export default {
  getData,
  postData
}